package csu.softwaremetric.controller;

import com.alibaba.fastjson.JSONObject;
import csu.softwaremetric.service.TXTUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class TXTUploadController {

    @Autowired
    private TXTUploadService txtUploadService;

    @PostMapping(value = "/txt")
    @ResponseBody
    public ResponseEntity<JSONObject> uploadXML(@RequestParam("file") MultipartFile txt) {
        JSONObject json = txtUploadService.uploadTXT(txt);
        String status = (String) json.get("STATUS");
        if (status.equals("SUCCESS")) {
            return new ResponseEntity<>(json, HttpStatus.OK);
        } else if (status.equals("FAILURE")) {
            return new ResponseEntity<>(json, HttpStatus.BAD_REQUEST);
        } else if (status.equals("ERROR")) {
            return new ResponseEntity<>(json, HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return null;
    }
}
